package gui;

import gui.browse.Browse;
import gui.util.GUIUtils;
import gui.util.JFrameCadastro;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Periferico;

import com.jgoodies.forms.layout.CellConstraints;

import controller.ManutencaoController;
import controller.PerifericoController;

public class PerifericoGUI extends JFrameCadastro {
	private static final long serialVersionUID = 1L;

	public JPanel panelCentral;
	public JButton botaoBrowse;
	public JTextField editCodigo;
	public JTextField editDescricao;

	public ActionListener actionIncluir;
	public ActionListener actionAlterar;
	public ActionListener actionExcluir;
	public ActionListener actionCancelar;
	public ActionListener actionGravar;

	public PerifericoGUI() {
		super();
		init();
	}

	public void init(){
		this.setTitle("Cadastro de Perifericos");
	}

	public JPanel getPanelCentral() {
		if (panelCentral == null){
			panelCentral = new JPanel(GUIUtils.getLayout(4, 15));
			CellConstraints  cc = new CellConstraints();

			panelCentral.add(new JLabel("Codigo"),		cc.xywh(  2, 4,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditCodigo(),			cc.xywh( 10, 4,  5, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(getBotaoBrowse(),			cc.xywh( 16, 4,  2, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(new JLabel("Descricao"),	cc.xywh(  2, 6,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditDescricao(),		cc.xywh( 10, 6, 19, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
		}
		return panelCentral;
	}

	public JTextField getEditCodigo() {
		if (editCodigo == null){
			editCodigo = new JTextField();
			editCodigo.setEditable(false);
		}
		return editCodigo;
	}
	public JTextField getEditDescricao() {
		if (editDescricao == null){
			editDescricao = new JTextField();
			editDescricao.setEnabled(false);
		}
		return editDescricao;
	}
	
	public JButton getBotaoBrowse(){
		return super.getBotaoBrowse(PerifericoController.getInstance());
	}

	@Override
	public ActionListener getActionIncluir() {
		if (actionIncluir == null){
			actionIncluir = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					limparDados();
					escreverRegistro(true);
					getEditCodigo().setText("0");
				}
			};	
		}
		return actionIncluir;
	}

	@Override
	public ActionListener getActionAlterar() {
		if (actionAlterar == null){
			actionAlterar = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					escreverRegistro(true);
				}
			};
		}
		return actionAlterar;
	}

	@Override
	public ActionListener getActionExcluir() {
		if (actionExcluir == null){
			actionExcluir = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (getEditCodigo().getText() != null && !getEditCodigo().getText().trim().isEmpty()){
						try {
							int opcao = (JOptionPane.showConfirmDialog(null, "Tem certeza que deseja excluir o registro "+ getEditCodigo().getText() +"?",
									"T�tulo", JOptionPane.YES_NO_OPTION));
							if (opcao == JOptionPane.YES_OPTION) {
								PerifericoController.getInstance().excluirPeriferico(Integer.valueOf(getEditCodigo().getText().trim()));
								limparDados();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			};
		}
		return actionExcluir;
	}

	@Override
	public ActionListener getActionCancelar() {
		if (actionCancelar == null){
			actionCancelar = new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					limparDados();
					escreverRegistro(false);
				}
			};
		}
		return actionCancelar;
	}

	@Override
	public ActionListener getActionGravar() {
		if (actionGravar == null){
			actionGravar = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (getEditCodigo().getText().trim() != null && !getEditCodigo().getText().trim().isEmpty()){
						if (getEditCodigo().getText().trim().equals("0")){
							try {
								PerifericoController.getInstance().inserirPeriferico(getEditDescricao().getText());
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
							try {
								PerifericoController.getInstance().alterarPeriferico(Integer.valueOf(getEditCodigo().getText().trim()), getEditDescricao().getText());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					escreverRegistro(false);
					limparDados();
					getBotaoFechar().grabFocus();
				}
			};
		}
		return actionGravar;
	}


	public void limparDados(){
		getEditCodigo().setText("");
		getEditDescricao().setText("");
	}
	public void escreverRegistro(boolean b){
		getBotaoCancelar().setEnabled(b);
		getBotaoGravar().setEnabled(b);
		getBotaoIncluir().setEnabled(!b);
		getBotaoAlterar().setEnabled(!b);
		getBotaoExcluir().setEnabled(!b);
		getBotaoFechar().setEnabled(!b);
		getBotaoBrowse().setEnabled(!b);
		getEditCodigo().setEnabled(!b);
		getEditDescricao().setEnabled(b);
		getEditDescricao().grabFocus();
	}

	@Override
	public void preencherCampos(HashMap<String, Object> dados) {
		Periferico p = null;
		try {
			p = PerifericoController.getInstance().buscarPerifericoPorId((Integer)dados.get("codigo"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		editCodigo.setText(String.valueOf(p.getId()));
		editDescricao.setText(p.getDescricao());
		
	}
}